
var path = require("path");
var fs = require("fs");
var cheerio = require('cheerio');

module.exports = function(config){

    return {
        compile:function(tpl_dir,app_module){
            var deployer = build_deployer(config,app_module);

            //var app_id = req.params["app_id"];
            var page_content = fs.readFileSync(tpl_dir);
            const $ = cheerio.load(page_content,{
                decodeEntities: false
            });

            $("[x-bind]").each(function(){
                var binds = $(this).attr("x-bind");
                //console.log(binds);
                if(binds){
                    binds = binds.split(".");
                }

                var d=app_module;
                for(var i=0;i<binds.length;i++){
                    d = d[binds[i]];
                }
                if(d){
                    if($(this).is("input")){
                        $(this).val(d);
                    }else{
                        $(this).html(d);
                    }
                    
                }                
            });


            $("script[x-holder='style']").after(deployer.write_styles()).remove();
            $("script[x-holder='script']").after(deployer.write_scripts()).remove();


            /*
            $("title").html(app_config.name);
            $("meta[name='description']").attr("content",app_config.description);

            $("head").append("<script>var global_app_name='"+app_config.app_name+"';var global_app_id='"+app_id+"';</script>");

            res.header("Content-Type","text/html");
            res.end($.html());
            */

            return $.html();
        }
    }
}

function build_deployer(config,app_module){
    var app_config = app_module.config;

    var templates = {};
    

    function write_styles(){
        var style_src = [];
        (app_config.styles||[]).forEach(function(style) {
            if(config.style_alias[style]){
                style = config.style_alias[style];
            }
            style_src.push("<link rel='stylesheet' href='"+style+"'></link>");
            
        });

        return style_src.join("\n");
    }

    function get_cpmt_path(app_name,cpmt_name){
        return "/platform/"+app_name+"/"+(app_config.component_dir||config.component_dir)+"/"+cpmt_name+".js";
    }

    function get_view_path(app_name,cpmt_name){
        return "/"+app_name+"/"+(app_config.view_dir||config.view_dir)+"/"+cpmt_name+".html";
    }

    function write_scripts(){
        var script_src = [];

        (config.base_lib||[]).forEach(function(script) {
            if(config.script_alias[script]){
                script = config.script_alias[script];
            }
            script_src.push("<script type='text/javascript' src='"+script+"'></script>");
        });

        
        //console.log(script_src);

        (app_config.libs||[]).forEach(function(script) {
            if(config.script_alias[script]){
                script = config.script_alias[script];
            }
            script_src.push("<script type='text/javascript' src='"+script+"'></script>");
        });

        //console.log(script_src);

        (app_config.components||[]).forEach(function(cpmt) {
            var cpmt_path;
            if(config.script_alias[cpmt]){
                cpmt_path = config.script_alias[cpmt];
            }else{
                cpmt_path = get_cpmt_path(app_module.app_name,cpmt);
            }
            script_src.push("<script type='text/javascript' src='"+cpmt_path+"'></script>");

            var view_path = get_view_path(app_module.app_name,cpmt);

            //console.log(config.root_dir,app_module.app_root || app_module.platform_root ,view_path);

            var view_content = fs.readFileSync(path.join(config.root_dir,app_module.app_root || app_module.platform_root ,view_path),'utf-8');
            script_src.push(view_content);

        });

        //console.log(script_src);

        return script_src.join("\n");
    }

    /*
    function write_template(){
        template
        var script_src = [];
        (app_config.libs||[]).forEach(function(script) {
            if(config.script_alias[script]){
                script = config.script_alias[script];
            }
            script_src.push("<script type='text/javascript' src='"+script+"'></script>");
        });

        (app_config.components||[]).forEach(function(cpmt) {
            if(config.script_alias[cpmt]){
                cpmt = config.script_alias[cpmt];
            }else{
                cpmt = get_cpmt_path(app_module.app_name,cpmt);
            }
            script_src.push("<script type='text/javascript' src='"+cpmt+"'></script>");
        });

        //console.log(script_src.join("\n"));

        return script_src.join("\n");
    }*/

    return  {
        write_styles:write_styles,
        write_scripts:write_scripts
    }
}
